package pl.edu.pw.mini.java2.Communicator.Client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class MainWindow extends JFrame implements ListSelectionListener,
		ActionListener, ClientListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Client client;
	private JLabel name;

	private JList contactList;
	private DefaultListModel listModel;

	private JButton btnConnect;

	private String currPair = "";

	public MainWindow(Client client) {

		this.client = client;
		client.addClientListener(this);

		initUI(client);
	}

	@Override
	public void getAvailableContacts(String name) {
		listModel.addElement(name);
		contactList.setModel(listModel);
	}

	@Override
	public void getLeavingContact(String name) {
		listModel.removeElement(name);
		contactList.setModel(listModel);
	}

	@Override
	public void chatInvitation(String name) {
		currPair = name;
		Runnable runner = new Runnable() {
			@Override
			public void run() {
				startChat();
			}
		};
		Thread t = new Thread(runner);
		t.start();
	}

	private void initUI(Client client) {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(200, 400);
		setMinimumSize(new Dimension(200, 400));

		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

		name = new JLabel(client.getLoginInfo().getLogin());
		name.setFont(new Font(Font.SANS_SERIF, Font.ITALIC, 20));
		btnConnect = new JButton("Connect");
		btnConnect.addActionListener(this);

		listModel = new DefaultListModel();

		contactList = new JList(listModel);
		contactList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		contactList.setSelectedIndex(0);
		contactList.addListSelectionListener(this);
		contactList.setVisibleRowCount(20);

		if (contactList.getSelectedIndex() > 0)
			currPair = (String) listModel.getElementAt(contactList
					.getSelectedIndex());

		setLayout(new BorderLayout());
		add(name, BorderLayout.NORTH);
		add(contactList, BorderLayout.CENTER);
		add(btnConnect, BorderLayout.SOUTH);
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		if (e.getValueIsAdjusting() == false)
			return;
		int i = contactList.getSelectedIndex();
		if (i < 0)
			return;
		currPair = (String) listModel.elementAt(i);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		try {
			client.sendChatRequest(currPair);
		} catch (Exception e1) {
			JOptionPane.showMessageDialog(this, currPair + " is busy!",
					"TITLE", JOptionPane.ERROR_MESSAGE);
			return;
		}
		startChat();
	}

	private void startChat() {
		ChatWindow chatWin = new ChatWindow();
		client.addChatListener(chatWin);
		chatWin.setClient(client);
		chatWin.setPairName(currPair);
		chatWin.setModal(true);
		chatWin.setTitle(client.getLoginInfo().getLogin() + " > " + currPair);
		chatWin.setVisible(true);
		client.addChatListener(null);
	}
}